local huo = fk.CreateSkill{
    name = "rmt__huo",
}
Fk:loadTranslationTable{
    ["rmt__huo"] = "活",
    [":rmt__huo"] = "一张<font color='red'>♥</font>进入弃牌堆后，你获得10点好感度。出牌阶段，或你受到1点伤害后，你可以消耗10好感度随机获取一块女性角色的武将碎片(4块合成完整武将，碎片从头、胸、腿、足中随机获得)，并摸该武将拼合进度张牌。",
    ["#rmt__huo-prompt"] = "是否发动 活，消耗10好感度随机获取一块女武将碎片",
    ["@rmt__huo"] = "好感度",
    ["@[rmt__xing]"] = "碎片背包",
    ["#rmt__huo1"] = "头部碎片",
    [":#rmt__huo1"] = "这是一块武将碎片……",
    ["#rmt__huo2"] = "胸部碎片",
    [":#rmt__huo2"] = "这是一块武将碎片……",
    ["#rmt__huo3"] = "腿部碎片",
    [":#rmt__huo3"] = "这是一块武将碎片……",
    ["#rmt__huo4"] = "足部碎片",
    [":#rmt__huo4"] = "这是一块武将碎片……",
    ["#rmt__huo-gain-log"] = "%from 发动〖%arg〗获得了 %arg2 的 <font color='#791cc0'>%arg3</font>",
}

Fk:addQmlMark{
    name = "rmt__xing",
    how_to_show = function(name, value)
        if type(value) == "table" then
            return tostring(#value)
        end
        return " "
    end,
    qml_path = "packages/aaa_Romantic/qml/rmt__xing"
}

local spec = {
    on_use = function (self, room, effect)
        local player = effect.from
        room:removePlayerMark(player,"@rmt__huo",10)

        local mark = player:getTableMark("@[rmt__xing]")
        local generals = {}
        for _, general in pairs(Fk.generals) do
            if general.gender == General.Female and not general.hidden and not general.total_hidden then
                table.insert(generals, general.name)
            end
        end
        local loop = true
        local num = 0

        while loop and num < 10 do
            num = num + 1
            local general = table.random(generals)
            local all_choices = {general,{}}
            local forLoop = true
            for _, value in ipairs(mark) do
                if value[1] == general and forLoop then
                    forLoop = false
                    all_choices = value
                end
            end
            local choices  = {}
            for _, v in ipairs({"#rmt__huo1","#rmt__huo2","#rmt__huo3","#rmt__huo4"}) do
                if not table.contains(all_choices[2],v) then
                    table.insertIfNeed(choices,v)
                end
            end
            if #choices > 0 then
                loop = false
                local randomChoic = table.random(choices)
                table.insertIfNeed(all_choices[2],randomChoic)
                table.sort(all_choices[2],function (a, b)
                    local a_last = tonumber(a:sub(-1))
                    local b_last = tonumber(b:sub(-1))
                    return a_last < b_last
                end)
                local has = false
                for index, value in ipairs(mark) do
                    if value[1] == general and has == false then
                        has = true
                        mark[index] = all_choices
                    end
                end
                if has == false then
                    table.insert(mark,all_choices)
                end
                room:sendLog{
                    type = "#rmt__huo-gain-log",
                    from = player.id,
                    arg = huo.name,
                    arg2 = general,
                    arg3 = randomChoic,
                    toast = true,
                }
                room:setPlayerMark(player,"@[rmt__xing]",mark)
                player:drawCards(#all_choices[2],huo.name)
            end
        end
    end,
}

huo:addEffect("active",{
    card_num = 0,
    target_num = 0,
    card_filter = Util.FalseFunc,
    target_filter = Util.FalseFunc,
    can_use = function (self, player)
        return player:getMark("@rmt__huo") >= 10
    end,
    prompt = "#rmt__huo-prompt",
    on_use = spec.on_use,
})

huo:addEffect(fk.Damaged,{
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(huo.name) and player == target and player:getMark("@rmt__huo") >= 10
    end,
    on_cost = function (self, event, target, player, data)
        if player.room:askToSkillInvoke(player,{
            skill_name = huo.name,
            prompt = "#rmt__huo-prompt"
        }) then
            return true
        end
    end,
    trigger_times = function (self, event, target, player, data)
        return data.damage
    end,
    on_use = function (self, event, target, player, data)
        --[[local skill = Fk.skills[huo.name] ---@type ActiveSkill
        skill:onUse(player.room, SkillUseData:new {
            from = player,
            cards = nil,
            tos = nil,
        })]]--
        spec.on_use(self,player.room, {from = player})
    end,
})

huo:addEffect(fk.AfterCardsMove, {
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(huo.name) then
            for _, move in ipairs(data) do
                if move.toArea == Card.DiscardPile then
                    for _, info in ipairs(move.moveInfo) do
                        if Fk:getCardById(info.cardId).suit == Card.Heart then
                            return true
                        end
                    end
                end
            end
        end
    end,
    on_cost = Util.TrueFunc,
    trigger_times = function (self, event, target, player, data)
        local n = 0
        for _, move in ipairs(data) do
            if move.toArea == Card.DiscardPile then
                for _, info in ipairs(move.moveInfo) do
                    if Fk:getCardById(info.cardId).suit == Card.Heart then
                        n = n + 1
                    end
                end
            end
        end
        return n
    end,
    on_use = function (self, event, target, player, data)
        player.room:addPlayerMark(player,"@rmt__huo",10)
    end,
})

return huo